#pragma once
#include <sys/epoll.h>
#include <sys/types.h>
#include "Socket.hpp"
#include "Logger.hpp"

using namespace LogMudule;
class Epollor
{
public:
    Epollor():_epfd(-1)
    {
    }
    void Iint()
    {
        _epfd = epoll_create(256);
        if(_epfd < 0)
        {
            LOG(LogLevel::ERROR)<<"epoll create";
            exit(EPOLL_CREAT_ERR);
        }
        LOG(LogLevel::DEBUG)<<"epoll create sucess :"<<_epfd;
    }
    int Wait(struct epoll_event res[],int num,int timeout)
    {
        int ret = epoll_wait(_epfd,res,num,timeout);
        if(ret < 0)
        {
            LOG(LogLevel::WARNING) << "epoll_wait error";
        }
        return ret;
    }
    void Ctl(int sockfd,uint32_t events,int flag)
    {
        struct epoll_event eve;
        eve.data.fd = sockfd;
        eve.events = events;
        int n = epoll_ctl(_epfd,flag,sockfd,&eve);
        if(n < 0)
        {   
            LOG(LogLevel::ERROR)<<"epoll ctl";
            exit(EPOLL_CTL_ERR);
        }
    }
    void Add(int sockfd,uint32_t events)
    {
        Ctl(sockfd,events,EPOLL_CTL_ADD);
    }
    void Del(int sockfd)
    {
        int n = epoll_ctl(_epfd,EPOLL_CTL_DEL,sockfd,nullptr);
        if(n < 0)
        {
            LOG(LogLevel::ERROR)<<"epoll ctl";
            exit(EPOLL_CTL_ERR);
        }
    }
    void Update(int sockfd,uint32_t events)
    {
        Ctl(sockfd,events,EPOLL_CTL_MOD);
    }
    ~Epollor()
    {
    }

private:
    int _epfd;
};